package home.study.xxy.bs.controller;

import home.study.xxy.bs.entity.LoginUser;
import home.study.xxy.bs.entity.Page;
import home.study.xxy.bs.entity.ResultList;
import home.study.xxy.bs.entity.ResultObj;
import home.study.xxy.bs.entity.User;
import home.study.xxy.bs.service.LoginService;
import home.study.xxy.bs.utils.CheckImageUtils;
import home.study.xxy.bs.utils.GetToken;
import home.study.xxy.bs.utils.MD5Utils;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @title: LoginController
 * @功能:
 * @Author Xiao Xiangyu
 * @Date: 2021/4/11 19:04
 * @Version 1.0
 */
@Slf4j
@RestController
public class LoginController {

    private StringBuffer rightString;
    @Autowired
    private LoginService loginService;


    @RequestMapping(value = "/test")
    public String test(){
        System.out.println("test   ...");
        return "test ok";
    }

    /**
     * 登录接口
     */
    @RequestMapping(value = "/login")
    public ResultObj loginController(User user, String number, HttpSession session) {
//        if (number == null) {
//            return null;
//        }
//        if (number.equalsIgnoreCase(rightString.toString())) {
//            if (loginService.checkLogin(user) >= 1) {
                ResultObj loginData = loginService.getLoginData(user);
                log.info(user.getUserName() + "登录成功");
                return loginData;
//            } else {
//                log.error("登录验证失败");
//            }
//        } else {
//            log.error("验证码错误");
//        }
//        return null;
    }

    /**
     * 查询所有用户
     */
    @RequestMapping("/getAll")
    public ResultList<User> queryAllUser(User user, Page pageVo) {
        return loginService.queryAll(user, pageVo);
    }

    /**
     * 新增注册用户
     */
    @RequestMapping("/addUser")
    public boolean addUser(User user) {
        if (user == null) {
            return false;
        }
        loginService.addUser(user);
        log.info("注册成功");
        return true;
//        if (){
//
//        }else{
//            log.info("注册失败");
//            return false;
//        }

    }

    /**
     * 修改密码，密码确认
     */
//    @GetMapping("/beforeChangePwd")
//    public boolean beforeChangePwd(String pwd) {
//        if (MD5Utils.stringToMD5(pwd).equals(pwdsession)){
//            return true;
//        }
//        return false;
//    }
    /**
     * 修改用户信息
     */
    @RequestMapping("/changePwd")
    public boolean changePwd(User user,String vipTimeString) {
        if (user == null) {
            return false;
        }
        if (vipTimeString!=null){

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//
            try {
                Date date = simpleDateFormat.parse(vipTimeString);
                user.setVipTime(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        int result = loginService.changePwd(user);
        if (result == 1) {
            log.info("修改信息成功");
            return true;
        } else {
            log.info("修改信息失败");
            return false;
        }

    }

    /**
     * 修改用户权限
     */
    @RequestMapping("/updateAdmin")
    public boolean updateAdmin(User user) {
        if (user == null) {
            return false;
        }
        int result = loginService.updateAdmin(user);
        if (result == 1) {
            log.info("修改权限成功");
            return true;
        } else {
            log.info("修改权限失败");
            return false;
        }

    }

    /**
     * 验证码生成
     */
    @RequestMapping(value = "/checkImage")
    public void CheckImageUtils(HttpServletResponse response) {
        CheckImageUtils imageUtils = new CheckImageUtils();
        try {
            ImageIO.write(imageUtils.dre(), "jpg", response.getOutputStream());
            rightString = imageUtils.getSb();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            log.error("验证码生成失败");
            e.printStackTrace();
        }
    }
}
